
import axios from 'axios';
axios.defaults.withCredentials = true;
axios.defaults.baseURL = process.env.VITE_API_URL;
export const service = axios.create({
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
    },
});

service.interceptors.request.use(
    (config) => {
        return config;
    },
    (error) => {
        console.log('请求错误', error);
        return Promise.reject(error);
    },
);


service.interceptors.response.use(
    (response) => {
        if (response.data.code === 500) {
            console.log(response.data)
            return Promise.reject(response.data);
        }

        return response;
    },
    (error) => {
        console.log('响应错误', error);

        return Promise.reject(error);
    },
);

const request = {
    get: (url: string, params?: any) => new Promise((resolve, reject) => {
        service.get(url, { params }).then((response) => {
            if (!response.data.ok) {
                console.log("服务响应错误信息", response.data.message)
                return;
            }
            resolve(response.data);
        }).catch((error) => {
            reject(error);
        });
    }),
    postJson: (url: string, data?: any, config?: any) => new Promise((resolve, reject) => {
        service.post(url, data, { headers: { 'Content-Type': 'application/json' }, ...config }).then((response) => {
            if (!response.data.ok) {
                console.log("服务响应错误信息", response.data.message)
                return;
            }
            resolve(response.data);
        }).catch((error) => {
            reject(error);
        });
    }),
    post: (url: string, data?: any, config?: any) => new Promise((resolve, reject) => {
        service.post(url, data, config).then((response) => {
            if (!response.data.ok) {
                console.log("服务响应错误信息", response.data.message)
                return;
            }
            resolve(response.data);
        }).catch((error) => {
            reject(error);
        });
    }),
    putJson: (url: string, data?: any, config?: any) => new Promise((resolve, reject) => {
        service.put(url, data, { headers: { 'Content-Type': 'application/json' }, ...config }).then((response) => {
            if (!response.data.ok) {
                console.log("服务响应错误信息", response.data.message)
                return;
            }
            resolve(response.data);
        }).catch((error) => {
            reject(error);
        });
    }),
    put: (url: string, data?: any, config?: any) => new Promise((resolve, reject) => {
        service.put(url, data, config).then((response) => {
            if (!response.data.ok) {
                console.log("服务响应错误信息", response.data.message)
                return;
            }
            resolve(response.data);
        }).catch((error) => {
            reject(error);
        });
    }),
    patchJson: (url: string, data?: any, config?: any) => new Promise((resolve, reject) => {
        service.patch(url, data, { headers: { 'Content-Type': 'application/json' }, ...config }).then((response) => {
            if (!response.data.ok) {
                console.log("服务响应错误信息", response.data.message)
                return;
            }
            resolve(response.data);
        }).catch((error) => {
            reject(error);
        });
    }),
    patch: (url: string, data?: any, config?: any) => new Promise((resolve, reject) => {
        service.patch(url, data, config).then((response) => {
            if (!response.data.ok) {
                console.log("服务响应错误信息", response.data.message)
                return;
            }
            resolve(response.data);
        }).catch((error) => {
            reject(error);
        });
    }),
    delete: (url: string, params?: any) => new Promise((resolve, reject) => {
        service.delete(url, { params }).then((response) => {
            if (!response.data.ok) {
                console.log("服务响应错误信息", response.data.message)
                return;
            }
            resolve(response.data);
        }).catch((error) => {
            reject(error);
        });
    }),
};
export default request;